package com.demo.project_template.service.impl;

import com.demo.project_template.constant.GoldEnum;
import com.demo.project_template.entity.Gift;
import com.demo.project_template.entity.User;
import com.demo.project_template.entity.UserGift;
import com.demo.project_template.entity.UserGoldRecord;
import com.demo.project_template.exception.ErrorCodeException;
import com.demo.project_template.mapper.UserGiftMapper;
import com.demo.project_template.mapper.UserMapper;
import com.demo.project_template.service.GiftService;
import com.demo.project_template.service.UserGiftService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.project_template.service.UserGoldRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杨玉龙
 * @since 2024-08-21 11:53:54
 */
@Service
public class UserGiftServiceImpl extends ServiceImpl<UserGiftMapper, UserGift> implements UserGiftService {
    @Resource
    private UserGoldRecordService userGoldRecordService;
    @Resource
    private GiftService giftService;
    @Resource
    private UserMapper userMapper;

    @Override
    public void sendGift(UserGift userGift) {
        //查询礼物并补充用户礼物
        Gift gift = giftService.getById(userGift.getGiftId());
        if (gift==null){
            throw new ErrorCodeException("礼物不存在");
        }
        userGift.setGiftName(gift.getName());
        userGift.setGiftPicture(gift.getPicture());
        userGift.setGiftPrice(gift.getPrice());

        User receiver = userMapper.selectById(userGift.getUserId());
        User sender = userMapper.selectById(userGift.getSendUserId());

        //送礼物的人进行扣费
        UserGoldRecord senderGoldRecord = new UserGoldRecord();
        senderGoldRecord.setUserId(userGift.getSendUserId());
        senderGoldRecord.setIsAdd("0");

        //收礼物的人添加金币
        UserGoldRecord receiverGoldRecord = new UserGoldRecord();
        receiverGoldRecord.setUserId(userGift.getUserId());
        receiverGoldRecord.setIsAdd("1");

        switch (gift.getType()){
                //普通礼物
            case "1":
                senderGoldRecord.setType(GoldEnum.SEND_GIFT_REDUCE.getCode());
                senderGoldRecord.setReason(String.format(GoldEnum.SEND_GIFT_REDUCE.getName(),receiver.getNickName(),gift.getName()));

                receiverGoldRecord.setType(GoldEnum.ACCEPT_GIFT_ADD.getCode());
                receiverGoldRecord.setReason(String.format(GoldEnum.ACCEPT_GIFT_ADD.getName(),sender.getNickName(),gift.getName()));
                break;
                //解锁微信
            case "2":
                senderGoldRecord.setType(GoldEnum.VIEW_WECHAT_REDUCE.getCode());
                senderGoldRecord.setReason(String.format(GoldEnum.VIEW_WECHAT_REDUCE.getName(),receiver.getNickName()));

                receiverGoldRecord.setType(GoldEnum.VIEW_WECHAT_ADD.getCode());
                receiverGoldRecord.setReason(String.format(GoldEnum.VIEW_WECHAT_ADD.getName(),sender.getNickName()));
                break;
                //畅聊好友
            case "3":
                senderGoldRecord.setType(GoldEnum.BECOME_FRIEND_REDUCE.getCode());
                senderGoldRecord.setReason(String.format(GoldEnum.BECOME_FRIEND_REDUCE.getName(),receiver.getNickName()));

                receiverGoldRecord.setType(GoldEnum.INVITE_FRIEND_ADD.getCode());
                receiverGoldRecord.setReason(String.format(GoldEnum.INVITE_FRIEND_ADD.getName(),sender.getNickName()));
                break;
                //查看相册
            case "4":
                senderGoldRecord.setType(GoldEnum.VIEW_PICTURE_REDUCE.getCode());
                senderGoldRecord.setReason(String.format(GoldEnum.VIEW_PICTURE_REDUCE.getName(),receiver.getNickName()));

                receiverGoldRecord.setType(GoldEnum.VIEW_PICTURE_ADD.getCode());
                receiverGoldRecord.setReason(String.format(GoldEnum.VIEW_PICTURE_ADD.getName(),sender.getNickName()));
                break;
                //实现心愿
            case "5":
                senderGoldRecord.setType(GoldEnum.ACHIEVE_WISH_REDUCE.getCode());
                senderGoldRecord.setReason(String.format(GoldEnum.ACHIEVE_WISH_REDUCE.getName(),receiver.getNickName()));

                receiverGoldRecord.setType(GoldEnum.ACHIEVE_WISH_ADD.getCode());
                receiverGoldRecord.setReason(String.format(GoldEnum.ACHIEVE_WISH_ADD.getName(),sender.getNickName()));
                break;
            default:
                throw new ErrorCodeException("参数错误");
        }


        userGoldRecordService.add(senderGoldRecord);
        userGoldRecordService.add(receiverGoldRecord);

        //收礼物的人添加礼物
        save(userGift);
    }
}
